﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Touch2Like.DAL;
using System.Data;
using Touch2Like.Model;

namespace Touch2Like.Web.Customer.Manage
{
    public partial class Coupon : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (WebUtility.UserGUID == null || WebUtility.UserGUID == "0")
            {
                Response.Clear();
                Response.Write("<script>parent.ResetURL('/Customer/signin.aspx?next=" + Server.UrlEncode("/Customer/Index.aspx") + "');</script>");
                Response.End();
            }

            if (!IsPostBack)
            {
                tb_CouponAllExpireDate.Text = FormatDate(DateTime.Now.AddMonths(1));
                tb_CouponPartialExpireDate.Text = FormatDate(DateTime.Now.AddMonths(1));

                BindData();
            }
        }

        string FormatDate(DateTime d)
        {
            string year = d.Year.ToString(), month = d.Month.ToString(), day = d.Day.ToString();
            if (d.Month.ToString().Length < 2) month = "0" + d.Month;
            if (d.Day.ToString().Length < 2) day = "0" + d.Day;
            return year + "-" + month + "-" + day;
        }

        string FormatShortDate(DateTime d)
        {
            string year = d.Year.ToString(), month = d.Month.ToString(), day = d.Day.ToString();
            return month + "/" + day;
        }

        void BindData()
        {
            UserDAL user = new UserDAL();
            DataTable dtSetting = user.GetSettingByUserGUID(WebUtility.UserGUID);

            CouponDesignDAL design = new CouponDesignDAL();
            CouponDesignInfo designInfo = null;

            if (dtSetting.Select("FlagName='CouponAllDesignID'").Length > 0)
            {
                int designid = Convert.ToInt32(dtSetting.Select("FlagName='CouponAllDesignID'")[0]["Value"]);
                designInfo = design.GetByID(designid);

                if (designInfo != null)
                {
                    tb_CouponAllExpireDate.Text = FormatDate(designInfo.ExpirationDate.Value);
                    tb_CouponAllValue.Text = designInfo.CouponValue;
                    img_CouponAll.Src = designInfo.CouponLocation;
                    tb_CouponAllLocation.Value = designInfo.CouponLocation;
                }
            }

            if (dtSetting.Select("FlagName='CouponPartialDesignID'").Length > 0)
            {
                int designid = Convert.ToInt32(dtSetting.Select("FlagName='CouponPartialDesignID'")[0]["Value"]);
                designInfo = design.GetByID(designid);

                if (designInfo != null)
                {
                    tb_CouponPartialExpireDate.Text = FormatDate(designInfo.ExpirationDate.Value);
                    tb_CouponPartialValue.Text = designInfo.CouponValue;
                    img_CouponPartial.Src = designInfo.CouponLocation;
                    tb_CouponPartialLocation.Value = designInfo.CouponLocation;
                }
            }
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            UserDAL user = new UserDAL();

            CouponDesignDAL design = new CouponDesignDAL();
            CouponDesignInfo designInfo = new CouponDesignInfo();

            designInfo.UserGUID = WebUtility.UserGUID;
            designInfo.CouponLocation = tb_CouponAllLocation.Value;
            if (file_CouponAll.HasFile)
            {
                string pathAll = "/Customer/assets/" + DateTime.Now.Ticks + ".jpg";
                file_CouponAll.PostedFile.SaveAs(Server.MapPath(pathAll));
                designInfo.CouponLocation = pathAll;
            }
            designInfo.CouponValue = tb_CouponAllValue.Text;
            designInfo.ExpirationDate = Convert.ToDateTime(tb_CouponAllExpireDate.Text);

            int allid = design.Add(designInfo);

            designInfo = new CouponDesignInfo();

            designInfo.UserGUID = WebUtility.UserGUID;
            designInfo.CouponLocation = tb_CouponPartialLocation.Value;
            if (file_CouponPartial.HasFile)
            {
                string pathPartial = "/Customer/assets/" + DateTime.Now.Ticks + ".jpg";
                file_CouponPartial.PostedFile.SaveAs(Server.MapPath(pathPartial));
                designInfo.CouponLocation = pathPartial;
            }
            designInfo.CouponValue = tb_CouponPartialValue.Text;
            designInfo.ExpirationDate = Convert.ToDateTime(tb_CouponPartialExpireDate.Text);

            int partialid = design.Add(designInfo);

            UserSettingInfo settingInfo = new UserSettingInfo();
            settingInfo.UserGUID = WebUtility.UserGUID;
            settingInfo.FlagName = "CouponAllDesignID";
            settingInfo.Value = allid.ToString();

            user.UpdateSetting(settingInfo);

            settingInfo = new UserSettingInfo();
            settingInfo.UserGUID = WebUtility.UserGUID;
            settingInfo.FlagName = "CouponPartialDesignID";
            settingInfo.Value = partialid.ToString();

            user.UpdateSetting(settingInfo);

            Response.Redirect("Coupon.aspx");
        }
    }
}